from urllib import request, parse
from lxml import etree
import ssl

# 取消 https 代理验证
ssl._create_default_https_context = ssl._create_unverified_context


def dyld(self, line):
    # 报头
    # user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36'
    user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
    headers = {'User-Agent': user_agent}
    url = ''
    dyld_url = ''
    if line == '1':
        dyld_url = "http://dianyingleida.com"
        url = dyld_url
    elif line == '2':
        dyld_url = "http://www.nmkan.com"
        url = dyld_url
    req = request.Request(url, headers=headers)
    try:
        response = request.urlopen(req)
        # 获取每页的HTML源码字符串
        html = response.read().decode('utf-8')
        # 解析html 为 HTML 文档
        selector = etree.HTML(html)

        # # 顶部nav
        # home_nav_list = selector.xpath('//header[@id="header-top"]//ul[@class="myui-header__menu nav-menu"]/li/a')[:-1]
        # home_nav_node_list = []
        # for node in home_nav_list:
        #     title = node.xpath('text()')[0]
        #     link = dyld_url + node.xpath('@href')[0]
        #     defalut_result = {
        #         "title": title,
        #         # "link": link,
        #     }
        #     home_nav_node_list.append(defalut_result)
        #
        # # 顶部二级nav
        # home_bar_list = selector.xpath('//header[@id="header-top"]//ul[@class="item nav-list clearfix"]/li/a')[:-1]
        # home_bar_node_list = []
        # for node in home_bar_list:
        #     title = node.xpath('text()')[0]
        #     link = dyld_url + node.xpath('@href')[0]
        #     defalut_result = {
        #         "title": title,
        #         # "link": link,
        #     }
        #     home_bar_node_list.append(defalut_result)
        #
        # navbar = {
        #     "nav": home_nav_node_list,
        #     "bar": home_bar_node_list,
        # }

        # 头部banner
        home_slide_list = selector.xpath('//div[@id="home_slide"]//div[contains(@class,"item")]/a')
        home_slide_node_list = []
        for node in home_slide_list:
            title = node.xpath('@title')[0]
            link = dyld_url + node.xpath('@href')[0]
            poster = node.xpath('img/@src')[0]
            if poster.startswith('/upload'):
                poster = dyld_url + poster
            defalut_result = {
                "title": title,
                "link": link,
                "poster": poster,
            }
            home_slide_node_list.append(defalut_result)

        # 今日推荐
        # recommended_num = selector.xpath('/html/body/div[3]/div/div[1]//span[contains(@class,"more")]')
        # home_slide_list = selector.xpath('/html/body/div[3]/div/div[1]')
        home_recommended = \
            selector.xpath('//div[@class="container"]//div[@class="myui-panel myui-panel-bg clearfix"]')[0:1][0]
        navbar_left = home_recommended.xpath('//h3[@class="title"]/text()')[0]
        more = home_recommended.xpath('//span[contains(@class,"more")]/text()')[0]
        home_recommended_list = home_recommended.xpath(
            'div[@class="myui-panel-box clearfix"]/div[@class="myui-panel_bd clearfix"]/ul[@class="myui-vodlist clearfix"]/li/div[@class="myui-vodlist__box"]')
        home_recommended_node_list = []
        for node in home_recommended_list:
            title = node.xpath('a/@title')[0]
            link = dyld_url + node.xpath('a/@href')[0]
            poster = node.xpath('a/@data-original')[0]
            if poster.startswith('/upload'):
                poster = dyld_url + poster
            definition = node.xpath('a/span[@class="pic-text text-right"]/text()')[0]
            intro = node.xpath('div[@class="myui-vodlist__detail"]/p/text()')[0]
            defalut_result = {
                "title": title,
                "link": link,
                "poster": poster,
                "definition": definition,
                "intro": intro,
            }
            home_recommended_node_list.append(defalut_result)
        recommended = {
            "navbar": navbar_left,
            "more": more,
            "list": home_recommended_node_list,
        }

        # 电影，电视剧，动漫，综艺等列表
        home_list = selector.xpath(
            '//div[@class="container"]//div[@class="myui-panel myui-panel-bg clearfix"]/div[@class="myui-panel-box clearfix"]/div[@class="myui-panel_bd clearfix"]')[
                    1:5]
        home_node_list = []
        for node in home_list:
            more = dyld_url + node.xpath('div[1]/div/div/a[contains(@class,"more")]/@href')[0]
            # 最新
            navbar_left = node.xpath('div[1]/div/div/h3[@class="title"]/text()')[0]
            list_left = node.xpath('div[1]/ul/li/div[@class="myui-vodlist__box"]')
            list_node_left = []
            for node_list_left in list_left:
                title = node_list_left.xpath('a/@title')[0]
                link = dyld_url + node_list_left.xpath('a/@href')[0]
                poster = node_list_left.xpath('a/@data-original')[0]
                if poster.startswith('/upload'):
                    poster = dyld_url + poster
                definition = node_list_left.xpath('a/span[@class="pic-text text-right"]/text()')[0]
                intro = node_list_left.xpath('div[@class="myui-vodlist__detail"]/p/text()')[0]
                defalut_result = {
                    "title": title,
                    "link": link,
                    "poster": poster,
                    "definition": definition,
                    "intro": intro,
                }
                list_node_left.append(defalut_result)
            left = {
                # "more": more,
                "navbar": navbar_left,
                "list": list_node_left,
            }
            # 最热
            navbar_right = node.xpath('div[2]/div/div/h3[@class="title"]/text()')[0]
            list_right = node.xpath('div[2]/ul/li/a')
            list_node_right = []
            for node_list_right in list_right:
                title = node_list_right.xpath('@title')[0]
                link = dyld_url + node_list_right.xpath('@href')[0]
                definition = node_list_right.xpath('span[contains(@class,"text-muted")]/text()')[0]
                defalut_result = {
                    "title": title,
                    "link": link,
                    "definition": definition,
                }
                list_node_right.append(defalut_result)
            right = {
                # "more": more,
                "navbar": navbar_right,
                "list": list_node_right,
            }
            defalut_tow_result = {
                "left": left,
                "right": right,
            }
            home_node_list.append(defalut_tow_result)
        result = {
            # "navbar": navbar,
            "slide": home_slide_node_list,
            "recommended": recommended,
            "home_list": home_node_list,
        }

        return result



    except Exception:
        print(url, Exception)
        data = {"code": -1, "message": "error", "data": None}
        return data
